<?php
//数据库类

final class Db
{

    protected $dbc;

    //构造函数
    public function __construct()
    {
        global $dbc;
        $this->dbc = $dbc;
    }

    //获取数据库连接
    public function getDbc()
    {
        if ($this->dbc == '') {
            global $dbc;
            $dbc = mysqli_connect(HOSTADDRESS, HOSTUSERNAME, HOSTPASSWORD, HOSTDB, 3306) or die("连接数据库失败！");
            $this->dbc = $dbc;
        }
        return $this->dbc;
    }

    //获取insert 的 ID
    public function getInsertId()
    {
        $id = mysqli_insert_id($this->dbc);
        return $id;
    }

    //插入某个表 返回插入id
    public function i($tableName, $keyValueArray)
    {
        $cols = '';
        $vals = '';
        foreach ($keyValueArray as $col => $val) {
            $cols .= " {$col} ,";
            if (is_numeric($val)) {
                $vals .= " {$val} ,";
            } else {
                $vals .= " '{$val}' ,";
            }
        }
        $cols = trim($cols, ',');
        $vals = trim($vals, ',');

        $q = "insert into {$tableName}( {$cols} ) values($vals)";
        $this->exec($q);
        return $this->getInsertId();
    }

    //更新数据 返回更新的数量
    public function u($query)
    {
        $result = $this->exec($query);
        $r = mysqli_affected_rows($this->dbc);
        return $r;
    }

    //不安全的获取数据
    public function gv($name)
    {
        return $_REQUEST[$name];
    }

    //获取数字简化版
    public function gn($name)
    {
        $v = $this->gv($name);
        if (is_numeric($v)) {
            return $v;
        } else {
            die($name . '=' . $v . ' is not number');
        }
    }

    //获取数字
    public function getnum($name)
    {
        $num = trim($_REQUEST[$name]);
        if (is_numeric($num)) {
            return $num;
        } else {
            throw new Exception($name . ' bu shi shu zi');
        }
    }

    //获取网络请求串
    public function getval($name)
    {
        return $this->safeString(trim($_REQUEST[$name]));
    }

    //获取安全字符串
    public function safeString($str)
    {
        $this->getDbc();
        return mysqli_real_escape_string($this->dbc, $str);
    }

    //执行sql语句
    public function exec($query)
    {
        if ($query == '' || $query == null || trim($query) == '') {
            die('exec query empty');
        } else {
            $this->getDbc();
            $result = mysqli_query($this->dbc, $query) or
            die(mysqli_error($this->dbc) . ' <br/> ' . PHP_EOL . $query);
            return $result;
        }
    }

    //从结果集获取一行
    public function result_getrow($result)
    {
        $row = mysqli_fetch_array($result);
        return $row;
    }

    //返回单行结果的数组
    public function query_getrow($query)
    {
        $r = $this->exec($query);
        $row = mysqli_fetch_array($r);
        return $row;
    }

    //
    public function query_getobj($query)
    {
        $r = $this->exec($query);
        $row = mysqli_fetch_object($r);
        return $row;
    }

    //
    public function result_getobj($result)
    {
        $r = $result;
        $obj = mysqli_fetch_object($r);
        return $obj;
    }

    //增加数据
    public function idu($query)
    {
        $result = $this->exec($query);
        if ($result) {
            echo '成功';
        } else {
            echo '失败';
        }
    }

    //插入数据 返回ID
    public function d($query)
    {
        $this->exec($query);
        $r = mysqli_affected_rows($this->dbc);
        return $r;
    }

    //插入数据 返回ID
    public function i_n($query)
    {
        $this->exec($query);
        return $this->getInsertId();
    }

    //查找数据
    public function s($query)
    {
        $result = $this->exec($query);
        $arr = array();
        while ($obj = mysqli_fetch_object($result)) {
            array_push($arr, $obj);
        }
        echo json_encode($arr);
    }

    //获取结果数量
    public function getshuliang($result)
    {
        return mysqli_num_rows($result);
    }

    //获取随机json结果
    public function getsuijiresult($max, $table_name, $where)
    {
        if (empty($where)) {
            $where = '1';
        }

        $query = "select count(*) from {$table_name} where {$where} ";
        $row = $this->query_getrow($query);
        $zongshu = $row['count(*)'];

        //总数不够
        if ($max >= $zongshu) {
            $query = "select * from {$table_name} where {$where} order by id desc limit {$max}  ";
            $this->s($query);
            return;
        }

        //随机数组
        $arr = array();
        for ($i = 0; $i < $max; ++$i) {
            $sjs = rand(0, $zongshu);
            while (array_search($sjs, $arr)) {
                $sjs = rand(0, $zongshu);
            }
            $arr[] = $sjs;
        }

        //对应数据
        $arr2 = array();
        for ($i = 0; $i < $max; ++$i) {
            $query = "select * from {$table_name} where {$where} limit 1 offset {$arr[$i]}";
            $res = $this->exec($query);
            $obj = mysqli_fetch_object($res);
            $arr2[] = $obj;
        }
        echo json_encode($arr2);
    }

}
